package com.ifonly.logger.jdk;

import com.ifonly.logger.Logger;

/**
 * @author <a href="mailto:ifonlymaster@163.com">ifonly</a>
 * @version 1.0 2015-12-09 10:40
 * @since JDK 1.6
 */
public class JDKLogger extends Logger {
    private final java.util.logging.Logger logger;

    public JDKLogger(String name) {
        this.name = name;
        this.logger = java.util.logging.Logger.getLogger(name);
    }

    public JDKLogger(Class<?> clazz) {
        this.name = clazz.getName();
        this.logger = java.util.logging.Logger.getLogger(this.name);
    }

    private java.util.logging.Level level2jdk(Level level) {
        switch (level) {
            case TRACE: return java.util.logging.Level.FINER;
            case DEBUG: return java.util.logging.Level.FINE;
            case INFO:	return java.util.logging.Level.INFO;
            case WARN:	return java.util.logging.Level.WARNING;
            case ERROR:	return java.util.logging.Level.SEVERE;
            default:
                throw new IllegalArgumentException();
        }
    }

    @Override
    public boolean isEnabled(Level level) {
        return logger.isLoggable(level2jdk(level));
    }

    @Override
    public void log(Level level, String message) {
        logger.log(level2jdk(level), message);
    }

    @Override
    public boolean isTraceEnabled() {
        return logger.isLoggable(java.util.logging.Level.FINER);
    }

    @Override
    public void trace(String message) {
        logger.log(java.util.logging.Level.FINER, message);
    }

    @Override
    public boolean isDebugEnabled() {
        return logger.isLoggable(java.util.logging.Level.FINE);
    }

    @Override
    public void debug(String message) {
        logger.log(java.util.logging.Level.FINE, message);
    }

    @Override
    public boolean isInfoEnabled() {
        return logger.isLoggable(java.util.logging.Level.INFO);
    }

    @Override
    public void info(String message) {
        logger.log(java.util.logging.Level.INFO, message);
    }

    @Override
    public boolean isWarnEnabled() {
        return logger.isLoggable(java.util.logging.Level.WARNING);
    }

    @Override
    public void warn(String message) {
        logger.log(java.util.logging.Level.WARNING, message);
    }

    @Override
    public void warn(String message, Throwable throwable) {
        logger.log(java.util.logging.Level.WARNING, message, throwable);
    }

    @Override
    public boolean isErrorEnabled() {
        return logger.isLoggable(java.util.logging.Level.SEVERE);
    }

    @Override
    public void error(String message) {
        logger.log(java.util.logging.Level.SEVERE, message);
    }

    @Override
    public void error(String message, Throwable throwable) {
        logger.log(java.util.logging.Level.SEVERE, message, throwable);
    }
}
